package com.example.esp8266;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

import com.example.esp8266.databinding.ItemBinding;

public class ItemView extends LinearLayout {

    private ItemBinding binding;
    private String title;
    private boolean enable;

    public ItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        binding = ItemBinding.inflate(LayoutInflater.from(context), this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemView);
        title = typedArray.getString(R.styleable.ItemView_text);
        enable = typedArray.getBoolean(R.styleable.ItemView_switchEnable, false);

        binding.title.setText(title);
        binding.switcher.setChecked(enable);
        typedArray.recycle();

    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    @BindingAdapter("title")
    public static void title(ItemView itemView,String title){
        itemView.setTitle(title);
    }


    @BindingAdapter("switchEnable")
    public static void title(ItemView itemView,boolean enable){
        itemView.setEnable(enable);
    }

    public void setSwitchListener(CompoundButton.OnCheckedChangeListener listener){
        binding.switcher.setOnCheckedChangeListener(listener);
    }
}
